package com.jn.retrofit.views;


import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.retrofit.R;
import com.jn.retrofit.adapter.HotSearchAdapter;
import com.jn.retrofit.contract.ApiService;
import com.jn.retrofit.entity.HotSearch;
import com.jn.retrofit.entity.TranslateRequest;
import com.jn.retrofit.entity.TranslateResponse;
import com.jn.retrofit.presenter.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private ApiService apiService;
    private RecyclerView recyclerView;
    private HotSearchAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // 确保这里引用的布局文件名正确

        apiService = RetrofitClient.getClient("https://api.oioweb.cn/")
                .create(ApiService.class);

        recyclerView = findViewById(R.id.recyclerView);
        adapter=new HotSearchAdapter(); // 传递上下文到适配器
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        findViewById(R.id.getHotSearch).setOnClickListener(v -> getHotSearch("baidu"));
        findViewById(R.id.postTranslate).setOnClickListener(v -> postTranslate());
    }

    private void getHotSearch(String type) {
        apiService.getHotSearch(type).enqueue(new Callback<HotSearch>() {
            @Override
            public void onResponse(Call<HotSearch> call, Response<HotSearch> response) {
                if (response.isSuccessful() && response.body() != null) {
                    adapter.setList(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<HotSearch> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void postTranslate() {
        TranslateRequest request = new TranslateRequest();
        request.setSourceText("Hello World");
        apiService.postTranslation(request).enqueue(new Callback<TranslateResponse>() {
            @Override
            public void onResponse(Call<TranslateResponse> call, Response<TranslateResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    displayTranslation(response.body());
                } else {
                    Toast.makeText(MainActivity.this, "Error: " + response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<TranslateResponse> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Network Error", Toast.LENGTH_SHORT).show();
                t.printStackTrace();
            }
        });
    }

    private void displayTranslation(TranslateResponse response) {
        Toast.makeText(this, response.getTargetText(), Toast.LENGTH_SHORT).show();
    }
}